﻿///<reference path="../../../../Scripts/jquery-1.4.1-vsdoc.js"/>


function SearchControl(id) {

    var thisId = id;
    this.Container;
    var searchTextBox;
    var searchButton;

    var container = $("<div>", { id: thisId, className: "searchPanel" });
    var searchResultPanel;
    var searchInputPanel;


    this.Render = function () {
        searchInputPanel = $("<div>", { className: "searchInputPanel" }).appendTo(container)
        searchResultPanel = $("<div>", { className: "searchResultPanel" }).appendTo(container);

        searchTextBox = $("<input>", {
            type: "text",
            className: "searchTextBox"
        });

        searchButton = $("<button>", {
            html: "Поиск",
            className: "searchButton smallText",
            onclick: function () {
                DoSearch();
            }
        });

        searchTextBox.keypress(function (event) {
            if (event.which == 13)
                searchButton.click();
        });

        searchInputPanel.append(searchTextBox).append(searchButton);
    }

    this.FocusSearchTextBox = function () {
        searchTextBox.focus();
    }

    this.GetContainer = function () {
        return container;
    }

    function DoSearch() {
        var searchPattern = searchTextBox.val();

        if (searchPattern.length < 3) {
            searchResultPanel.html("Укажите более двух символов");
        }
        else {
            searchResultPanel.html("Поиск...");
            var results = [];


            AddRange(results, DoSearchInBible(searchPattern));            

            if (results.length == 0)
                searchResultPanel.html("Ничего не найдено");
            else {
                searchResultPanel.html("");
                var resultsDiv = $("<div>");
                for (var i = 0; i < results.length; i++) {
                    resultsDiv.append(results[i]);
                }

                searchResultPanel.append(resultsDiv);  // лучше добавлять в DOM один элемент, чем много
                searchResultPanel.highlight(searchPattern);
            }
        }

        searchTextBox.focus();
    }

    function DoSearchInBible(searchPattern) {
        var verses = appPointer.BiblePlayer.Search(searchPattern.toLowerCase());
        var results = [];
        for (var i = 0; i < verses.length; i++) {
            var verse = verses[i];
            var result = $("<p class='searchResultItem clickable' onclick='appPointer.CommandProcessor.SetHash(\""
                 + appPointer.KeyManager.GetChapterKeyFromVerseKey(verse.GetKey())
                 + "\", function () { appPointer.MainPaneController.HighlightVerse(\"" 
                 + verse.GetKey() + "\", \"" + searchPattern + "\") });' />");            
            result.append($("<span>", { className: "boldText smallText", html: verse.GetFullName()}));
            result.append($("<span>", { className: "smallText blockText", html: verse.GetText() }));
            
            results.push(result);
        }

        return results;

    }

  
}